package com.bckj.fastboot.iot.data.mongo.repository;

import com.bckj.fastboot.core.extra.mongo.AbstractMongoRepository;
import com.bckj.fastboot.core.extra.mongo.MongoOps;
import com.bckj.fastboot.core.lang.model.PageResult;
import com.bckj.fastboot.iot.common.dto.query.IotStatusDataQuery;
import com.bckj.fastboot.iot.common.dto.request.IotStatusDataRequest;
import com.bckj.fastboot.iot.common.dto.response.IotStatusDataResponse;
import com.bckj.fastboot.iot.common.repository.IotStatusDataRepository;
import com.bckj.fastboot.iot.data.mongo.convert.IotStatusDataConvert;
import com.bckj.fastboot.iot.data.mongo.entity.IotStatusData;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * IotStatusDataRepositoryImpl
 *
 * @author wfc
 **/
@Repository
public class IotStatusDataRepositoryImpl
    extends AbstractMongoRepository<IotStatusData, Long>
        implements IotStatusDataRepository  {

    private final IotStatusDataConvert iotStatusDataConvert;

    public IotStatusDataRepositoryImpl(
            MongoOps mongoOps,
            IotStatusDataConvert iotStatusDataConvert
    ) {
        super(mongoOps, IotStatusData.class, "iot_status_data");
        this.iotStatusDataConvert = iotStatusDataConvert;
    }

    @Override
    public boolean save(IotStatusDataRequest request) {
        var entity = iotStatusDataConvert.toEntity(request);
        return save(entity);
    }

    @Override
    public IotStatusDataResponse details(Long id) {
        return findById(id, IotStatusDataResponse.class);
    }

    @Override
    public List<IotStatusDataResponse> findList(IotStatusDataQuery query) {
        return find(buildQuery(query), IotStatusDataResponse.class);
    }

    @Override
    public PageResult<IotStatusDataResponse> page(Pageable page, IotStatusDataQuery query) {
        return page(page, buildQuery(query), IotStatusDataResponse.class);
    }

    private Query buildQuery(IotStatusDataQuery query) {
        Criteria criteria = Criteria.where("devCode").is(query.getDevCode());
        return Query.query(criteria);
    }
}
